﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;

using $safeprojectname$.Service;
using $safeprojectname$.Tools.Convert;

namespace $safeprojectname$
{
    /// <summary>
    ///     Conversion Manager provides some application specific functionality
    ///     (Number to Word)
    /// </summary>
    public static class ConversionManager
    {
        /// <summary>
        ///     Takes a number and returns its word representation
        /// </summary>
        /// <param name="number"></param>
        /// <param name="culture"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string NumberToWord(this long number, CultureInfo culture, string separator = null)
        {
            var converter = Instance.GetByCulture(culture.TwoLetterISOLanguageName)
                         ?? Instance.GetDefault();

            if (converter.IsNull())
            {
                return string.Empty;
            }

            return converter.ConvertToWord(number, separator);
        }
        /// <summary>
        ///     Takes a number and returns its word representation
        /// </summary>
        /// <param name="number"></param>
        /// <param name="culture"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string NumberToWord(this int number, CultureInfo culture, string separator = null)
        {
            return ((long)number).NumberToWord(culture, separator);
        }

        #region lazy instance
        // beforefieldinit off
        // see: http://www.yoda.arachsys.com/csharp/singleton.html
        // see: http://www.yoda.arachsys.com/csharp/beforefieldinit.html
        static class Instance
        {
            // beforefieldinit
            const string NumberToWordConverter = "NumberToWordConverter";
            static readonly IEnumerable<INumberToWordConverter> NumberToWords;
            static readonly INumberToWordConverter Default;
            static Instance()
            {
                NumberToWords = Factory.CreateInstances<INumberToWordConverter>();
                Default = NumberToWords
                    .FirstOrDefault(converter => converter.GetType().Name.IsEqual(NumberToWordConverter))
                    ?? NumberToWords.FirstOrDefault();
            }

            public static INumberToWordConverter GetByCulture(string cultureName)
            {
                return NumberToWords
                    .FirstOrDefault(converter => converter.GetType().Name
                        .StartsWith(NumberToWordConverter + cultureName, StringComparison.OrdinalIgnoreCase));
            }
            public static INumberToWordConverter GetDefault()
            {
                return Default;
            }
        }
        #endregion
    }
}